<?php
/**
* @copyright Copyright (C) 2007 Fiji Web Design. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @author gabe@fijiwebdesign.com
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

// register content event handlers
$_MAMBOTS->registerFunction( 'onPrepareContent', 'bot_tidy' );

/**
*  Tidy up the xHTML of your content
*/
function bot_tidy( $published, &$row, &$params, $page=0 ) {
	
	if ($published) {
		// get the plugin parameters
		//$botParams = bot_tidy_getParams('bot_tidy');

		if (isset($row->text) && $row->text) {
			$row->text = bot_tidy_parse($row->text);
		}

	}
	return true;
}

/**
* Retrieves parameters for a plugin
* @param plugin name (value of 'element' column in #__mambots db table)
*/
function bot_tidy_parse($html) {
	if (function_exists('tidy_parse_string')) {
		
		// Specify configuration
		$config = array(
				   'indent'         => true,
				   'output-xhtml'   => true,
				   'wrap'           => 200);
		// get Joomla content encoding
		$iso = split( '=', _ISO );
		$encoding = '';
		$jos_enc = str_replace('-', '', $iso[1]);
		if (in_array($jos_enc, array('ascii', 'latin0', 'latin1', 'raw', 'utf8', 'iso2022', 'mac', 'win1252', 'ibm858', 'utf16', 'utf16le', 'utf16be', 'big5', 'shiftjis'))) {
			$encoding = $jos_enc;
		}
		
		// Tidy
		$html = tidy_repair_string($html, $config, $encoding);
		
		return $html
		."\r\n<!-- cleaned with bot_tidy, http://www.fijiwebdesign.com/tags/bot_tidy/ -->"
		;
	} else {
		return $html
		."\r\n<!-- tidy is not available, http://www.fijiwebdesign.com/tags/bot_tidy/ -->"
		;
	}
}

/**
* Retrieves parameters for a plugin
* @param plugin name (value of 'element' column in #__mambots db table)
*/
function bot_tidy_getParams($plugin) {
	global $database, $_MAMBOTS;

	if ( !isset($_MAMBOTS->_content_mambot_params[$plugin]) ) {
		// load mambot params info
		$query = "SELECT params"
		. "\n FROM #__mambots"
		. "\n WHERE element = '$plugin'"
		. "\n AND folder = 'content'"
		;
		$database->setQuery( $query );
		$database->loadObject($mambot);

		// save query to class variable
		$_MAMBOTS->_content_mambot_params[$plugin] = $mambot;
	}

	$mambot = $_MAMBOTS->_content_mambot_params[$plugin];

 	$botParams = new mosParameters( $mambot->params );
	return $botParams;
}



?>